/*
 * Copyright 2011 Peter Kuterna
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.quiz.activity.sections;

import java.io.InputStream;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.Visibility;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.quiz.activity.R;
import com.quiz.activity.util.QuizUtil;
import com.quiz.dao.SectionDataSource;
import com.quiz.model.Section;

public class SectionFragment extends Fragment {

    private Section section;
    private Context context;
    private ViewGroup root;
    private SectionDataSource datasource;
    private Bitmap bitmap;
    private ImageView sectionImageView;

    public SectionFragment() {
    }

    public SectionFragment(Context context, Section section) {
        this.section = section;
        this.context = context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        root = (ViewGroup) inflater.inflate(R.layout.section_fragment, null);
        datasource = new SectionDataSource(this.context);
        //loadSectionImage(inflater);
        return root;
    }

    /**
     * Load the correct image for a section.
     * Determine if the image should show LOCKED or not and then find out if the user
     * should be able to tap the image to play a section.
     */
    private void loadSectionImage() {


        sectionImageView = (ImageView) root.findViewById(R.id.sectionImage);
        String imageName = section.getImageName();
        imageName = imageName.replace(".png", "");

        //Only make the image clickable if the section is Unlocked
        if (section.getStatus().equals("OPEN")) {
            sectionImageView.setOnClickListener(new StartQuestionsClickListener(this.context, section));
        } else {
            imageName = imageName + "_locked";
            TextView tapToPlayText = (TextView) root.findViewById(R.id.tapToPlayText);
            tapToPlayText.setVisibility(View.INVISIBLE);
        }

        InputStream is = getClass().getResourceAsStream("/res/drawable/" + imageName + ".png");
        bitmap = getBitMap(is);
        sectionImageView.setImageBitmap(bitmap);
        is = null;
    }

    private Bitmap getBitMap(InputStream is) {
        return BitmapFactory.decodeStream(is);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onResume() {
        super.onResume();
        datasource = new SectionDataSource(this.context);
        datasource.open();
        //Set the score details
        section = datasource.getSectionById(section.getId());
        TextView scoreDetailsView = (TextView) root.findViewById(R.id.scoreDetails);
        if (section.getStatus().equals("OPEN")) {
            scoreDetailsView.setText("Topscore " + QuizUtil.formatScore(section.getTopScore()));
        } else if (section.getStatus().equals("LOCKED")) {
            scoreDetailsView.setText("To unlock, you need " + QuizUtil.formatScore(section.getScoreNeeded()));
        }
        loadSectionImage();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onPause() {
        super.onPause();
        datasource.close();
        //Need to force a recycle of the bitmap when the activity is paused. This is to free up memory.
        bitmap.recycle();
        this.sectionImageView.setImageBitmap(null);
        bitmap = null;
    }
}
